package com.xyh.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyh.commonutils.R;
import com.xyh.educms.entity.CrmBanner;
import com.xyh.educms.service.CrmBannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin
public class BannerAdminController {
    @Resource
    private CrmBannerService bannerService;

    //1.分页查询 banner
    @GetMapping("/pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable("current") Long current, @PathVariable("limit") Long limit) {
        Page<CrmBanner> crmBannerPage = new Page<>(current, limit);
        bannerService.page(crmBannerPage, null);
        long total = crmBannerPage.getTotal();
        List<CrmBanner> records = crmBannerPage.getRecords();
        return R.ok().data("total", total).data("items", records);
    }

    //2.添加 banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    @GetMapping("/get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @PutMapping("/update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }
}

